#include <iostream>
#include <vector>
using namespace std;

int max_happy(vector<int> &head, vector<int> &next, vector<int> &to, vector<int> &happy, int start, vector<int> &recd)
{
        if(head[start] == 0){
                return happy[start];
        }

        if(recd[start] != -1){
                return recd[start];
        }

        int happy1 = happy[start];
        for(int e = head[start]; e; e = next[e]){
                int u = to[e];
                for(int ne = head[u]; ne; ne = next[ne]){
                        happy1 += max_happy(head, next, to, happy, to[ne], recd);
                }
        }

        int happy2 = 0;
        for(int e = head[start]; e; e = next[e]){
                happy2 += max_happy(head, next, to, happy, to[e], recd);
        }

        return recd[start] = max(happy1, happy2);
}

pair<int, int> get_happy(vector<int> &head, vector<int> &next, vector<int> &to, vector<int> &happy, int start)
{
        pair<int, int> info = {0, happy[start]};
        for(int e = head[start]; e; e = next[e]){
                int u = to[e];
                pair<int, int> uinfo = get_happy(head, next, to, happy, u);
                info.first += max(uinfo.first, uinfo.second);
                info.second += uinfo.first;
        }
        return info;
}

int main()
{
        int n;
        cin >> n;
        vector<int> happy(n + 1, 0);
        for(int i = 1; i <= n; i++){
                cin >> happy[i];
        }

        vector<int> head(n + 1, 0);
        vector<int> next(n + 1, 0);
        vector<int> to(n + 1, 0);
        vector<int> indgree(n + 1, 0);
        vector<int> recd(n + 1, -1);

        int u = 0, v = 0, cnt = 1;
        for(int i = 0; i < n - 1; i++){
                cin >> v >> u;
                next[cnt] = head[u];
                to[cnt] = v;
                head[u] = cnt++;
                indgree[v]++;
        }

        int start = 0;
        for(int i = 1; i <= n; i++){
                if(indgree[i] == 0){
                        start = i;
                        break;
                }
        }
        //cout<<max_happy(head, next, to, happy, start, recd)<<endl;
        auto [no, yes] = get_happy(head, next, to, happy, start);
        cout<<max(no, yes)<<endl;
        return 0;
}